package com.shoulder.boot.system.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shoulder.core.utils.CharacterUtils;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 列信息
 */
@Data
public class FieldTO {
    /**
     * 字段名
     */
    @NotEmpty(message = "字段名不能为空")
    private String name;
    /**
     * 字段类型
     */
    @NotEmpty(message = "字段类型不能为空")
    private String type;
    /**
     * 长度
     */
    private Integer length;
    /**
     * 允许为空
     */
    private Boolean required = false;
    /**
     * 字段备注
     */
    private String remark;
    /**
     * 字段默认值
     */
    private String defaultValue;

    @JsonIgnore
    private String javaType;

    @JsonIgnore
    private String javaField;

    public void buildJavaType() {
        switch (this.type.toLowerCase()) {
            case "bigint":
                this.javaType = "Long";
                break;
            case "datetime":
                this.javaType = "LocalDateTime";
                break;
            case "int":
                this.javaType = "Integer";
                break;
            case "bit":
                this.javaType = "Boolean";
                break;
            case "numeric":
                this.javaType = "BigDecimal";
                break;
            case "varchar", "text":
            default:
                this.javaType = "String";
                break;
        }
    }


    public static List<FieldTO> toList(List<FieldTO> list) {
        return list.stream().peek(item -> {
            item.buildJavaType();
            item.setJavaField(CharacterUtils.underscoreToCamelCase(item.getName()));
        }).collect(Collectors.toList());
    }
}
